package com.hzx.demo.activity;

import android.support.v7.app.AppCompatActivity;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.hzx.demo.R;
import com.hzx.demo.util.Entry;
import com.hzx.demo.util.HZXLog;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by ldkxingzhe on 2016/5/18.
 */
@Entry(desc = "动态在List前部添加子项, 但是不滑动. 点击标题添加", createTime = "2016-05-18 15:34")
@EActivity(R.layout.activity_add_item_list_view)
public class AddItemListViewActivity extends AppCompatActivity{
    @SuppressWarnings("unused")
    private static final String TAG = "AddItemListViewActivity";

    @ViewById(R.id.lv_list_view)
    protected ListView mListView;

    private List<String> mList = new ArrayList<>(Arrays.asList(new String[]{"ldk", "wyp", "zs", "zs", "zyy", "ljj", "wxm", "dqy", "zyj", "zpp", "隆恩", "gaga"}));
    private ArrayAdapter<String> mAdapter;
    @Override
    protected void onStart() {
        super.onStart();
        HZXLog.v(TAG, "onStart");
        HZXLog.v(TAG, "mListView.isStackFromBottom, %s.", mListView.isStackFromBottom());
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mList);
        mListView.setAdapter(mAdapter);
    }

    @Click(R.id.tv_title)
    public void onCLickTitle(){
        HZXLog.v(TAG, "onClickTitle");
        mList.add(0, "明月");
        mList.add(0, "明月是天涯的刀");
        mList.add(0, "天涯是明月的域");
        try {
            Field field = AdapterView.class.getDeclaredField("mFirstPosition");
            field.setAccessible(true);
            int currentPosition = field.getInt(mListView);
            field.set(mListView, currentPosition + 3);
            HZXLog.v(TAG, "currentPosition is %d.", currentPosition);
        } catch (NoSuchFieldException e) {
            HZXLog.e(TAG, e.getMessage(), e);
        } catch (IllegalAccessException e) {
            HZXLog.e(TAG, e.getMessage(), e);
        }
        mAdapter.notifyDataSetChanged();
    }
}
